package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.DeviceService;
import com.the4man.cnbdw.service.EmployeeDeviceHistoryService;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.DataGrid;
import com.the4man.framework.model.EmployeeDeviceHistory;
import com.the4man.framework.model.EmployeeDeviceHistoryExample;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Action(
        value = "employeeDeviceHistoryAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/employeeDeviceHistory/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/employeeDeviceHistory/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/employeeDeviceHistory/edit.jsp"
        )}
)
public class EmployeeDeviceHistoryAction extends BaseAction implements ModelDriven<EmployeeDeviceHistory> {
    private EmployeeDeviceHistory employeeDeviceHistory = new EmployeeDeviceHistory();
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private EmployeeDeviceHistoryService employeeDeviceHistoryService;
    @Autowired
    private EmployeeService employeeService;

    public EmployeeDeviceHistoryAction() {
    }

    public String indexPage() {
        String empId = this.getRequest().getParameter("id");
        String deviceId = this.getRequest().getParameter("deviceId");
        this.getRequest().setAttribute("emp", empId);
        this.getRequest().setAttribute("device", deviceId);
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            EmployeeDeviceHistoryExample employeeDeviceHistoryExample = new EmployeeDeviceHistoryExample();
            String empId;
            if(StringUtils.isNotBlank(this.getRequest().getParameter("devId"))) {
                empId = this.deviceService.selectByName(this.getRequest().getParameter("devId"));
                employeeDeviceHistoryExample.createCriteria().andDevIdEqualTo(empId);
            }

            if(StringUtils.isNotBlank(this.getRequest().getParameter("device"))) {
                employeeDeviceHistoryExample.createCriteria().andDevIdEqualTo(this.getRequest().getParameter("device"));
            }

            if(StringUtils.isNotBlank(this.getRequest().getParameter("empId"))) {
                empId = this.employeeService.selectByName(this.getRequest().getParameter("empId"));
                employeeDeviceHistoryExample.createCriteria().andEmpIdEqualTo(empId);
            }

            if(StringUtils.isNotBlank(this.getRequest().getParameter("emp"))) {
                employeeDeviceHistoryExample.createCriteria().andEmpIdEqualTo(this.getRequest().getParameter("emp"));
            }

            this.employeeDeviceHistoryService.countByExample(employeeDeviceHistoryExample);
            Page<EmployeeDeviceHistory> page = new Page();
            this.setPageProperties(page);
            this.employeeDeviceHistoryService.selectByExample(employeeDeviceHistoryExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)page.getRowCount()));
            this.writeJson(d);
        } catch (Exception var5) {
            LOG.error("获取数据失败", var5, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            String time = DateUtil.getInstance().formatDate(new Date());
            this.employeeDeviceHistoryService.insert(this.employeeDeviceHistory);
            json.setReturnFlag(true);
            json.setReturnMsg("提交成功！");
        } catch (Exception var3) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var3, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            this.employeeDeviceHistoryService.updateByPrimaryKey(this.employeeDeviceHistory);
            json.setReturnFlag(true);
            json.setReturnMsg("提交成功！");
        } catch (Exception var3) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var3, new String[0]);
        }

        this.writeJson(json);
    }

    public String executeModifyResult() {
        try {
            Object instance = this.employeeDeviceHistoryService.selectByPrimaryKey(this.employeeDeviceHistory.getId(), EmployeeDeviceHistory.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int i = this.employeeDeviceHistoryService.deleteByPrimaryKey(this.getRequest().getParameter("ids"), EmployeeDeviceHistory.class);
            if(i > 0) {
                json.setReturnMsg("删除EmployeeDeviceHistory_info成功！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除EmployeeDeviceHistory_info失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var3) {
            LOG.error("批量删除EmployeeDeviceHistory_info失败！", var3, new String[0]);
            json.setReturnMsg("删除EmployeeDeviceHistory_info失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public EmployeeDeviceHistory getModel() {
        return this.employeeDeviceHistory;
    }
}
